package com.ch.android.lookforkotlinv2.vm

import android.app.Application
import android.content.Context
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ch.android.networklib.RequestClient
import com.ch.android.networklib.data.LiveListItemData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * Author: conghao
 * Date: 2023/12/28 15:26
 * Description: LiveListModel
 */
class LiveListModel(application: Application) : AndroidViewModel(application) {
    val items = MutableLiveData<List<LiveListItemData>>(mutableListOf())
    val error = MutableLiveData<Throwable>()

    fun loadData(liveTypeId: Int, page: Int, size: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                val baseData = RequestClient.getApiService(getApplication()).getLiveList(liveTypeId, page, size)
                baseData.result?.let {
                    withContext(Dispatchers.Main) {
                        items.value = it
                    }
                }
            } catch (e: Exception) {
                error.value = e
            }
        }
    }

}